/*
 * Copyright (C) 2010 http://laantiguafragua.blogspot.com/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * SFUShutdownAgent.java
 *
 * Created on 04/03/2010
 */

import java.io.File;

/**
 *
 * @author Lhrod
 */
public class SFUShutdownAgent {

    final static String SEP1 = "\n";
    final static String SEP2 = "\t";
    final static int MAX_ATTEMPT = 4;
    final static int TIME_SLEEP = 100;

    public static void main(String args[]) {
        String delStrings = System.getenv("deletefiles");
        String renStrings = System.getenv("renamefiles");

        int attempt = 0;

        for (String delFilePath : delStrings.split(SEP1)) {
            File f = new File(delFilePath);
            while (f.exists() && (attempt < MAX_ATTEMPT)) {
                if (!f.delete()) {
                    try {
                        Thread.sleep(TIME_SLEEP);
                    } catch (InterruptedException ex) {
                        System.err.println(ex);
                    }
                }
                attempt++;
            }
        }
        for (String renFilePath : renStrings.split(SEP1)) {
            String[] ren = renFilePath.split(SEP2);
            File f = new File(ren[0]);
            File fTmp = new File(ren[1]);
            f.renameTo(fTmp);
        }
    }
}
